from django.contrib.auth.models import Group
from django.core.exceptions import PermissionDenied

def is_admin_user(user):
    """检查用户是否是管理员"""
    return user.groups.filter(name='Administrators').exists() or user.is_superuser

class AdminPermissionMixin:
    """管理员权限混合类"""
    
    def dispatch(self, request, *args, **kwargs):
        if not is_admin_user(request.user):
            raise PermissionDenied("您没有访问管理后台的权限")
        return super().dispatch(request, *args, **kwargs)

def create_admin_group():
    """创建管理员用户组"""
    group, created = Group.objects.get_or_create(name='Administrators')
    return group
